﻿using HR60_PanelSoft.Model;
using HR60_PanelSoft.NetWork.Base;
using HR60_PanelSoft.Tools;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HR60_PanelSoft.NetWork.Sender
{
    class Sender_0102:BaseSender
    {
        public Sender_0102(byte panelNo, byte sceneNum, Scene scene, Device device)
        {
            byte[] data = new byte[128]; //32个分机


            List<Device> listDevice = device.childDevices;
            int cur = 0;
            foreach (Device cd in listDevice)
            {

                SceneEvent se = scene.findEventByDevice(cd.uuid);
                //1.无
                if (se == null)
                {
                    cur = cur + 4;
                    continue;
                }

                List<string> sArrts = se.attrs;

                List<string> values = se.attrValueKeys;


                DeviceType type = cd.type;
                List<CtrlAttr> attrs = type.attrs;
                if (attrs == null)
                {
                    cur = cur + 4;
                    continue;
                }

                if (attrs.Count != 2)
                {
                    throw new Exception("数据内容有问题,请查看地暖属性是否配置正确？");
                }


                if (attrs.Count == 2)
                {

                    string binaryString = "1";//待拼接的字符串

                    //1
                    {

                        CtrlAttr attr0 = attrs[0];
                        int index = sArrts.IndexOf(attr0.name);
                        string value0 = null;
                        if (index != -1)
                        {
                            value0 = values[index];
                        }


                        if (value0 == null)  //开关机
                        {
                            binaryString = binaryString + "0";
                        }
                        else
                        {
                            binaryString = binaryString + value0;
                        }

                    }
                   
                    //2

                    {
                        CtrlAttr attr1 = attrs[1];
                        int index = sArrts.IndexOf(attr1.name);
                        string value1 = null;
                        if (index != -1)
                        {
                            value1 = values[index];
                        }

                        if (value1 == null)
                        {
                            binaryString = binaryString + "00000";
                        }
                        else if (value1 == "1")
                        {
                            binaryString = binaryString + "00001";
                        }
                        else if (value1 == "2")
                        {
                            binaryString = binaryString + "00010";
                        }
                        else if (value1 == "3")
                        {
                            binaryString = binaryString + "00011";
                        }
                        else if (value1 == "4")
                        {
                            binaryString = binaryString + "00100";
                        }
                        else if (value1 == "5")
                        {
                            binaryString = binaryString + "00101";
                        }
                        else if (value1 == "6")
                        {
                            binaryString = binaryString + "00110";
                        }
                        else if (value1 == "7")
                        {
                            binaryString = binaryString + "00111";
                        }
                        else if (value1 == "8")
                        {
                            binaryString = binaryString + "01000";
                        }
                        else if (value1 == "9")
                        {
                            binaryString = binaryString + "01001";
                        }
                        else if (value1 == "10")
                        {
                            binaryString = binaryString + "01010";
                        }
                        else if (value1 == "11")
                        {
                            binaryString = binaryString + "01011";
                        }
                        else if (value1 == "12")
                        {
                            binaryString = binaryString + "01100";
                        }
                        else if (value1 == "13")
                        {
                            binaryString = binaryString + "01101";
                        }
                        else if (value1 == "14")
                        {
                            binaryString = binaryString + "01110";
                        }
                        else if (value1 == "15")
                        {
                            binaryString = binaryString + "01111";
                        }
                        else if (value1 == "16")
                        {
                            binaryString = binaryString + "10000";
                        }
                        else if (value1 == "17")
                        {
                            binaryString = binaryString + "10001";
                        }
                        else if (value1 == "18")
                        {
                            binaryString = binaryString + "10010";
                        }
                        else if (value1 == "19")
                        {
                            binaryString = binaryString + "10011";
                        }
                        else if (value1 == "20")
                        {
                            binaryString = binaryString + "10100";
                        }
                        else if (value1 == "21")
                        {
                            binaryString = binaryString + "10101";
                        }
                        else if (value1 == "22")
                        {
                            binaryString = binaryString + "10110";
                        }
                        else if (value1 == "23")
                        {
                            binaryString = binaryString + "10111";
                        }
                        else if (value1 == "24")
                        {
                            binaryString = binaryString + "11000";
                        }
                        else if (value1 == "25")
                        {
                            binaryString = binaryString + "11001";
                        }
                        else if (value1 == "26")
                        {
                            binaryString = binaryString + "11010";
                        }
                        else if (value1 == "27")
                        {
                            binaryString = binaryString + "11011";
                        }
                        else if (value1 == "28")
                        {
                            binaryString = binaryString + "11100";
                        }
                        else if (value1 == "29")
                        {
                            binaryString = binaryString + "11101";
                        }
                        else if (value1 == "30")
                        {
                            binaryString = binaryString + "11110";
                        }

                    }


                   

                    data[cur++] = Convert.ToByte(binaryString, 2);

                    data[cur++] = 0;
                    data[cur++] = 0;
                    data[cur++] = 0;
                }


            }

            //构造package包
            this.package = Package.getPackage(0x01, 0x02, panelNo, sceneNum, data);



        }

    }
}
